package com.unohacha.qqj.mylibrary.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.trello.rxlifecycle2.components.support.RxFragment;

import java.io.Serializable;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @description: BaseFragment
 * @data 2018/1/26-11:11
 * @author: AoJiaoQiang
 */
public abstract class BaseFragment extends RxFragment {

    private View mRootView;
    private Unbinder mUnbinder;
    private Bundle mBundle;
    private LayoutInflater mInflater;
    private Toast toast;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = getArguments();
        initBundle(mBundle);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mInflater = inflater;
        if (mRootView != null) {
            // 防止布局重复加载
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
        } else {
            mRootView = inflater.inflate(getLayoutId(), container, false);
            mUnbinder = ButterKnife.bind(this, mRootView);
        }
        initView(mRootView);
        initData();
        return mRootView;
    }

    protected LayoutInflater getInflater(){
        return mInflater;

    }
    protected View getRootView(){
        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 销毁view的时候解除view绑定
        mUnbinder.unbind();
    }

    protected abstract void initBundle(Bundle arguments);
    protected abstract int getLayoutId();
    protected abstract void initView(View rootView);
    protected abstract void initData();

    protected <T extends Serializable> T getBundleSerializable(String key) {
        if (mBundle == null) {
            return null;
        }
        return (T) mBundle.getSerializable(key);
    }

    protected void jumpToOtherActivity(Context context,Class clazz){
        Intent intent = new Intent(context,clazz);
        startActivity(intent);
    }
    /**
     * 获取状态栏高度,在页面还没有显示出来之前
     *
     * @param a
     * @return
     */
    public  int getStateBarHeight(Activity a) {
        int result = 0;
        int resourceId = a.getResources().getIdentifier("status_bar_height",
                "dimen", "android");
        if (resourceId > 0) {
            result = a.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public void MyToast(Context context,String s) {
        if (toast == null)
            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
        else
            toast.setText(s);
        toast.show();
    }


}
